package com.zhanghe.study.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * 基础测试
 * @author zh
 * @date 2021/1/7 10:08
 */
@Controller
@RequestMapping("helloWorld")
// 设置name属性共享
@SessionAttributes(value={"name"})
public class HelloWorldController {


    // 视图解析器将该返回值进行解析， prefix + 返回值 + 后缀来得到实际返回的页面，然后进行转发
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("hello请求成功");
        return "hello";
    }

    @RequestMapping("/modelAndViewTest")
    public ModelAndView modelAndViewTest(){
        // 视图名
        ModelAndView modelAndView = new ModelAndView("hello");
        // 包含的数据
        modelAndView.addObject("dateTime",new Date());
        return modelAndView;
    }

    @RequestMapping("/mapTest")
    public String mapTest(Map<String,String> map){
        System.out.println(map.getClass()); //class org.springframework.validation.support.BindingAwareModelMap
        map.put("name","张三");
        return "hello";
    }

    // 可以在该方法中获取到name值为张三
    @RequestMapping("/sessionAttributes")
    public String sessionAttributes(HttpSession session){
        System.out.println(session.getAttribute("name"));
        return "hello";
    }
}
